// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'

Vue.config.productionTip = false
/*创建axios对象*/
let http=axios.create();
//设置请求拦截器:会在每一次真的发送请求之前执行，这里将token设置到请求头
//达到每次请求自动携带token
http.interceptors.request.use(
  config =>{
    console.log("请求前执行")
    //1.得到token
    let token =window.sessionStorage.getItem("token");
    //2.判断，如果不为空，则设置到请求头里面
    if (token){
      config.headers.authorization=token;
    }
    //请求放行
    return config;

  }
);
//响应拦截器，判断响应头中是否携带token
http.interceptors.response.use(
  response =>{
    console.log("请求后执行")
    //1.从响应头获取token'
    let token=response.headers.authorization;
    if (token){
      window.sessionStorage.setItem("token",token);
    }
    return response;
  }
);


Vue.prototype.axios=http
Vue.prototype.baseUrl="http://localhost:8080/"



/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
